import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        token: sessionStorage.getItem('token') ? sessionStorage.getItem('token') : null,
        userInfo: localStorage.getItem('userInfo') ? JSON.parse(localStorage.getItem('userInfo')) : null,
        collect: localStorage.getItem('collect') ? JSON.parse(localStorage.getItem('collect')) : null,
        address: localStorage.getItem('address') ? JSON.parse(localStorage.getItem('address')) : null
    },
    mutations: {
        addToken(state, value) {
            state.token = value
        },
        addUserInfo(state, value) {
            state.userInfo = value
        },
        addCollect(state, value) {
            state.collect = value
        },
        saveAddress(state, value) {
            state.address = value
        }
    },
    actions: {
        setToken(context) {
            context.commit('addToken', null)
            sessionStorage.removeItem('token')
        },
        setUserInfo(context) {
            context.commit('addUserInfo', null)
            localStorage.removeItem('userInfo')
        },
        setCollect(context) {
            context.commit('addCollect', null)
            localStorage.removeItem('collect')
        },
        setAddress(context) {
            context.commit('saveAddress', null)
            localStorage.removeItem('address')
        }
    },
    modules: {}
})
